﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTAProDataImporter.Transport;
using HTAProDataImporter.DataProvider;
using System.Data.OleDb;
using BLL.Entities;

namespace HTAProDataImporter
{
    public class DataImporter
    {
        public Guid ActiveCompanyID;
        public List<string> ErrorLog;
        public List<string> WarningLog;
        public ImportDataStore store;
        

        public DataImporter()
        {
            store = new ImportDataStore() { ActiveCompanyID = ActiveCompanyID};
            ErrorLog = new List<string>();
            WarningLog = new List<string>();
        }

        public void Run()
        {
            InitDynamicEnums();
            
            List<BaseEntityTransport> transports = new List<BaseEntityTransport>();

            transports.Add(new ICONTransport() { DataStore = store, Errors = ErrorLog, Warnings = WarningLog });
            transports.Add(new CompanyTransport() { DataStore = store, Errors = ErrorLog, Warnings = WarningLog });
            transports.Add(new AgentTransport() { DataStore = store, Errors = ErrorLog, Warnings = WarningLog });
            transports.Add(new JudgeTransport() { DataStore = store, Errors = ErrorLog, Warnings = WarningLog });
            transports.Add(new PoliceOfficerTransport() { DataStore = store, Errors = ErrorLog, Warnings = WarningLog });
            transports.Add(new CourtTransport() { DataStore = store, Errors = ErrorLog, Warnings = WarningLog });
            transports.Add(new AccountTypeTransport() { DataStore = store, Errors = ErrorLog, Warnings = WarningLog });
            transports.Add(new BankAccountTransport(){ DataStore = store, Errors = ErrorLog, Warnings = WarningLog });
            transports.Add(new ContactMethodTransport() { DataStore = store, Errors = ErrorLog, Warnings = WarningLog });
            transports.Add(new ContactMethodTransport() { DataStore = store, Errors = ErrorLog, Warnings = WarningLog });

            
            foreach(BaseEntityTransport t in transports)
                HTAProProvider.GetCollection(t);
        }

      

        private void InitDynamicEnums()
        {
            InitEnum_Prosecutor();
            InitEnum_BankAccountType();
            InitEnum_Gender();
            InitEnum_ContactMethod();
        }

        private void AddValuesToEnum(DynamicEnumType Holder, string[] Values)
        {
            foreach (string s in Values)
            {
                DynamicEnumTypeValue v1 = DynamicEnumTypeValue.CreateNewInstance<DynamicEnumTypeValue>();
                v1.EnumTypeID = Holder.EntityID;
                v1.Value = s;
                Holder.Values.Add(v1.Value, v1);
            }
        }

        private void InitEnum_ContactMethod()
        {
            DynamicEnumType contact_type = DynamicEnumType.CreateNewInstance<DynamicEnumType>();
            contact_type.IsGlobal = true;
            contact_type.TypeName = "ContactMethodType";

            //AddValuesToEnum(contact_type, new string[]{"Discrete", "Email", "Fax", "Phone", "Mail"} );

            store.ContactMethodType = contact_type;
        }
       

        private void InitEnum_Gender()
        {
            DynamicEnumType gender_type = DynamicEnumType.CreateNewInstance<DynamicEnumType>();
            gender_type.IsGlobal = true;
            gender_type.TypeName = "GenderType";

            AddValuesToEnum(gender_type, new string[] { "Male", "Female" });

            store.GenderType = gender_type;
        }

        private void InitEnum_BankAccountType()
        {
            DynamicEnumType account_type = DynamicEnumType.CreateNewInstance<DynamicEnumType>();
            account_type.IsGlobal = true;
            account_type.TypeName = "BankAccountType";

           // AddValuesToEnum(account_type, new string[] { "General Account", "Trust Account" });

            store.BankAccountType = account_type;
        }

        private void InitEnum_Prosecutor()
        {
            DynamicEnumType prosecutor_type = DynamicEnumType.CreateNewInstance<DynamicEnumType>();
            prosecutor_type.IsGlobal = true;
            prosecutor_type.TypeName = "CourtProsecutorType";

            AddValuesToEnum(prosecutor_type, new string[] {"Part 1 (Municipal) Prosecutor", "Part 3 (Provincial) Prosecutor"});

            store.CourtProsecutorType = prosecutor_type;
        }
    }
}
